package com.threeonemeeting.task;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.model.UserInfo;

// 注册帐号
public class RegisterTask extends ApplicationTask {

	public RegisterTask(Context context, ICallBack callback) {
		super(context, callback);

		this.nTaskType = HttpConst.TASKTYPE_REGISTER;
		this.strUrl = "http://new.31huiyi.com/json/RegisterUserAccountHandler.ashx";
	}

	// 具体的task实现该方法，对响应报文进行解析
	protected Map<String, Object> parseResponse(String strResponse) {

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put(HttpConst.KEY_TASK_TYPE, nTaskType);
		mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_SUCCESS);

		JSONObject jsonResp = null;
		try {
			jsonResp = new JSONObject(strResponse);
		} catch (Exception e) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		// 没有找到Setting段
		try {
			// 获取 "Setting"部分的内容
			String strSuccess = jsonResp.getString("success");
			if (strSuccess == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			if (strSuccess.equalsIgnoreCase("true") == false) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			// 读取data段
			JSONObject dataJson = jsonResp.getJSONObject("data");
			if (dataJson == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			// 读取result和error
			String strResult = dataJson.getString("result");

			// 出错了
			if (strResult.equalsIgnoreCase("error") == true) {
				setErr(Const.AppError.ERROR_REGISTER, mapData);
				String strErrorMsg = dataJson.getString("error");
				mapData.put(HttpUtil.KEY_ERROR_MSG, strErrorMsg);
				return mapData;
			}

			// 没出错，成功了
			String strNickName = dataJson.getString("nickname");
			String strRealName = dataJson.getString("realname");
			String strEmail = dataJson.getString("email");
			String strMobile = dataJson.getString("mobile");
			Integer nUserId = dataJson.getInt("userid");
			String strLastLogin = dataJson.getString("lastlogin");
			Integer nCoin = dataJson.getInt("coin");
			Long nCharge = dataJson.getLong("charge");
			String strCity = dataJson.getString("city");
			String strPassword = dataJson.getString("password");

			UserInfo currentUserInfo = DataCenter.getInstance().currentLoginUserInfo;
			currentUserInfo.strNickName = strNickName;
			currentUserInfo.strRealName = strRealName;
			currentUserInfo.strEmail = strEmail;
			currentUserInfo.strMobile = strMobile;
			currentUserInfo.nUserId = nUserId;
			currentUserInfo.strLastLoginTime = strLastLogin;
			currentUserInfo.nCoin = nCoin;
			currentUserInfo.fCharge = nCharge;
			currentUserInfo.strCity = strCity;
			currentUserInfo.strPassword = strPassword;

			setErr(HttpUtil.ERROR_SUCCESS, mapData);
			return mapData;

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}
	}
}
